<template>
  <div class="seller-container" @dblclick="revertMap">
    <div class="sellerchart" ref="seller_ref"></div>
  </div>
</template>

<script>
import axios from "axios";
import { getProvinceMapInfo } from "@/utils/map_utils";
export default {
  name: "Map",
  data() {
    return {
      chartInstance: null,
      allData: "",
      mapData: {},
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    clearInterval(this.timerId);
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    async initChart() {
      this.chartInstance = this.$echart.init(this.$refs.seller_ref, "customed");
      const ret = await axios.get(
        "http://localhost:8989/static/map/china.json"
      );
      this.$echart.registerMap("china", ret.data);
      this.chartInstance.on("click", async (arg) => {
        const provinceInfo = getProvinceMapInfo(arg.name);
        console.log(provinceInfo);
        if (!this.mapData[provinceInfo.key]) {
          const ret = await axios.get(
            "http://localhost:8989" + provinceInfo.path
          );
          this.mapData[provinceInfo.key] = ret.data;
          this.$echart.registerMap(provinceInfo.key, ret.data);
        }
        
        const changeOption = {
          geo: {
            map: provinceInfo.key,
          },
        };
        changeOption && this.chartInstance.setOption(changeOption);
      });
    },
    async getData() {
      const { data: ret } = await this.$http.get("map");
      this.allData = ret;
      console.log(this.allData);
      this.updateChart();

      const initOption = {
        title: {
          text: "| 商家分布",
          left: 20,
          top: 20,
        },
        legend: {
          left: "5%",
          bottom: "5%",
          orient: "vertical",
        },
        geo: {
          type: "map",
          map: "china",
          top: "6%",
          bottom: "6%",
          itemStyle: {
            areaColor: "#fd3f3f",
            borderColor: "#333",
          },
        },
      };
      initOption && this.chartInstance.setOption(initOption);
    },
    updateChart() {
      const seriesArr = this.allData.map((item) => {
        return {
          type: "effectScatter",
          name: item.name,
          data: item.children,
          coordinateSystem: "geo",
          rippleEffect: {
            scale: 5,
            brushType: "stroke",
          },
        };
      });
      const legendArr = this.allData.map((item) => {
        return item.name;
      });
      const dataOption = {
        series: seriesArr,
        legend: {
          data: legendArr,
        },
      };
      dataOption && this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const title = (this.$refs.seller_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: title,
          },
        },
        legend: {
          itemWidth: title / 2,
          itemHeight: title / 2,
          textStyle: {
            fontSize: title / 4,
          },
        },
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    revertMap() {
      const revertOption = {
        geo: {
          map: "china",
        },
      };
      revertOption && this.chartInstance.setOption(revertOption);
    },
  },
};
</script>

<style>
</style>